package Thread;

import java.util.concurrent.CountDownLatch;

public class demo4 {
    public static void main(String[] args) throws InterruptedException {
        CountDownLatch countDownLatch = new CountDownLatch(10);
        for (int i = 0; i <10 ; i++) {
            int id = i;
            Thread t =new Thread(()->{
                System.out.println("线程"+id+"开始工作！");
                try{
                    //使用sleep代指某些耗时操作，比如下载
                    Thread.sleep(2000);

                }
                catch (InterruptedException e){
                    e.printStackTrace();
                }
                System.out.println("线程"+id+"结束工作！");
                //每个任务执行结束后，调用一下方法
                //把10个线程想象成短跑比赛的10个运动员，countDown就是运动员撞线了
                countDownLatch.countDown();
            });
            t.start();
        }
        countDownLatch.await();
        System.out.println("多个线程的所有任务都执行完毕了！！");

    }
}
